//
//  HReplayProgressView.m
//  linktuIOS
//
//  Created by hejunsong on 2017/1/16.
//  Copyright © 2017年 tianxie. All rights reserved.
//

#import "HReplayProgressView.h"
#import "Masonry.h"
@interface HReplayProgressView()
@property(nonatomic,strong) UILabel *leftlable;
@property(nonatomic,strong) UIProgressView *progressView;
@property(nonatomic,strong) UILabel *rightLable;
@property(nonatomic,strong) UISlider *progressSlider;

@property(nonatomic,strong) UIView *bagView;

@property(nonatomic,strong) UIButton *playButton;

@end

@implementation HReplayProgressView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeUI];
    }
    return self;
}

-(void)makeUI
{
//    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    
    _bagView = [UIView new];
    
    _leftlable = [UILabel new];
    _rightLable = [UILabel new];

    
    _leftlable.font = [UIFont systemFontOfSize:10];
    _rightLable.font = [UIFont systemFontOfSize:10];
    _leftlable.textAlignment = NSTextAlignmentCenter;
    _rightLable.textAlignment = NSTextAlignmentCenter;
    
    _leftlable.textColor = [UIColor whiteColor];
    _rightLable.textColor = [UIColor whiteColor];
    
    _leftlable.text = @"00:00";
    _rightLable.text = @"00:00";
    
    _playButton = [UIButton new];
    [_playButton setImage:[UIImage imageNamed:@"icon_pause"] forState:UIControlStateSelected];
    [_playButton setImage:[UIImage imageNamed:@"icon_player"] forState:UIControlStateNormal];
    
    //slider
    self.progressSlider = [[UISlider alloc]init];
    self.progressSlider.minimumValue = 0.0;
    self.progressSlider.maximumValue = 1.0;
    self.progressSlider.value = 0;
    //    [self.progressSlider setThumbImage:[UIImage imageNamed:WMPlayerSrcName(@"dot")] ?: [UIImage imageNamed:WMPlayerFrameworkSrcName(@"dot")]  forState:UIControlStateNormal];  progress_playback@2x  button_Playbackhlive
    [self.progressSlider setThumbImage:[UIImage imageNamed:@"progress_playback"]  forState:UIControlStateNormal];
    self.progressSlider.minimumTrackTintColor = mainTintColor;
    self.progressSlider.maximumTrackTintColor = [UIColor grayColor];
    
    self.progressSlider.value = 0.0;//指定初始值
    //进度条的拖拽事件
    [self.progressSlider addTarget:self action:@selector(stratDragSlide:)  forControlEvents:UIControlEventValueChanged];
    //进度条的点击事件
    [self.progressSlider addTarget:self action:@selector(updateProgress:) forControlEvents:UIControlEventTouchUpInside];
    _bagView.layer.cornerRadius  = 20;
    _bagView.layer.masksToBounds = YES;
    [self addSubview:_bagView];
    
    [_bagView addSubview:_leftlable];
    [_bagView addSubview:_progressSlider];
    [_bagView addSubview:_rightLable];
    [self addSubview:_playButton];
    
    [_playButton addTarget:self action:@selector(playOrPausClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _bagView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    
    [_bagView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(self.mas_left).offset(60);
         make.right.mas_equalTo(self.mas_right).offset(6);
         make.top.mas_equalTo(self.mas_top).offset(0);
         make.bottom.mas_equalTo(self.mas_bottom).offset(-6);
        
    }];
    
    
    [_leftlable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_bagView.mas_centerY).offset(0);
        make.left.mas_equalTo(_bagView.mas_left).offset(10);
        make.width.mas_equalTo(@40);
    }];
    [_rightLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_bagView.mas_centerY).offset(0);
        make.right.mas_equalTo(_bagView.mas_right).offset(-10);
        make.width.mas_equalTo(@40);
    }];
    
    [_progressSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_bagView.mas_centerY).offset(0);
        make.right.mas_equalTo(_rightLable.mas_left).offset(-5);
        make.left.mas_equalTo(_leftlable.mas_right).offset(5);
    }];
    
    [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.width.mas_equalTo(@(40));
        make.height.mas_equalTo(@(40));
        make.centerY.mas_equalTo(self.mas_centerY).offset(0);
        
    }];
    

}
- (void)stratDragSlide:(UISlider *)slider{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(ReplayProgressViewstratDragSlider)])
    {
        [self.delegate ReplayProgressViewstratDragSlider];
    }
//    self.isDragingSlider = YES;
//    self.isDragingSlider = NO;
    
}
#pragma mark
#pragma mark - 播放进度
- (void)updateProgress:(UISlider *)slider{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(ReplayProgressViewupdateProgress:)])
    {
        CGFloat seconds = self.totaleTimeScocnd * slider.value;
        [self.delegate ReplayProgressViewupdateProgress:seconds];
    }
}


-(void)setCurrentTime:(NSInteger)currentTime
{
    _currentTime = currentTime;
    self.leftlable.text = [self showTotalTimeExchange:currentTime];
    self.progressSlider.value = ((CGFloat)_currentTime / (CGFloat)_totaleTimeScocnd);
    
    
}
-(void)setTotalTime:(NSInteger)totalTime
{
    _totalTime = totalTime;
    self.rightLable.text = [self showTotalTimeExchange:totalTime];
}


-(void)playOrPausClick:(UIButton *)but
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(ReplayProgressViewreplayplayorpause:)])
    {
        [self.delegate ReplayProgressViewreplayplayorpause:but.selected];
    }
    if(but.selected) {but.selected = NO;}else { but.selected = YES;}
    
}

-(void)setIsplay:(BOOL)isplay
{
    _isplay = isplay;
    self.playButton.selected = isplay;
}


- (NSString *)showTotalTimeExchange:(NSInteger)replay_duration
{
    int second = replay_duration % 60;
    double minutes = replay_duration / 60;
    //    double hours = replay_duration /3600;
    return [NSString stringWithFormat:@"%02.f:%02d",minutes,second];
}

-(void)setTotaleTimeScocnd:(NSInteger)totaleTimeScocnd
{
    _totaleTimeScocnd = totaleTimeScocnd;
    self.rightLable.text = [self showTotalTimeExchange:totaleTimeScocnd];
}

@end
